/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getFinanceList,
  getQuoteList,
  checkQuotationName,
  kuaidiBrands,
  getQuotationDetail,
  setInnQuotation,
  deleteCheck,
  quotationDelete,
  quotationSave,
} from "@/services/orderFinance";
import { message } from "antd";

export default {
  namespace: "order_finance",
  state: {
    quoteList: [],
    financeList: {},
    quoteTemp: {},
  },

  effects: {
    *getFinanceList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getFinanceList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            financeList: {
              list: data,
              pagination: {
                total: 20,
                current: 1,
                pageSize: 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getQuoteList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getQuoteList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            quoteList: data,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *checkQuotationName({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(checkQuotationName, payload);
      const { code, msg='名称重复' } = response;
      if (code > 0) {
        message.error(msg);
        __dva_reject();
        return false;
      }
      __dva_resolve(true);
      return true;
    },
    *kuaidiBrands({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(kuaidiBrands, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getQuotationDetail({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getQuotationDetail, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *setInnQuotation({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(setInnQuotation, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *deleteCheck({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(deleteCheck, payload);
      if (!response) return;
      __dva_resolve(response);
    },
    *quotationDelete({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(quotationDelete, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *quotationSave({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(quotationSave, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 保存临时数据
    saveTemp: (state, { payload }) => {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
